﻿namespace HiMap.MapControls
{
    using HiMap.Carto;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class LayerControlForCombox : UserControl
    {
        private MapControl _Map;
        private IContainer components;
        private ImageList img;
        private bool isEx;
        private LayerControl lc;
        private Panel panMain;
        private PictureBox picDown;
        private TextBox txt;

        public event TreeViewEventHandler LayerSelect;

        public LayerControlForCombox()
        {
            this.InitializeComponent();
            this.SizeLayerControl();
            this.LayerSelect = new TreeViewEventHandler(this.OnLayerSelect);
            this.isEx = false;
            this.picDown.Image = this.img.Images[0];
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        public void DownClick()
        {
            this.picDown_Click(null, null);
        }

        public ILayer GetLayer()
        {
            return this.lc.GetLayer();
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(LayerControlForCombox));
            this.panMain = new Panel();
            this.txt = new TextBox();
            this.picDown = new PictureBox();
            this.img = new ImageList();
            this.panMain.SuspendLayout();
            base.SuspendLayout();
            this.panMain.Controls.Add(this.txt);
            this.panMain.Controls.Add(this.picDown);
            this.panMain.Dock = DockStyle.Fill;
            this.panMain.Location = new Point(0, 0);
            this.panMain.Name = "panMain";
            this.panMain.Size = new Size(0xb2, 0x15);
            this.txt.Dock = DockStyle.Fill;
            this.txt.Location = new Point(0, 0);
            this.txt.Name = "txt";
            this.txt.ReadOnly = true;
            this.txt.Size = new Size(0x9d, 0x15);
            this.txt.TabIndex = 4;
            this.picDown.BackColor = Color.FromArgb(0xe0, 0xe0, 0xe0);
            this.picDown.Dock = DockStyle.Right;
            this.picDown.Location = new Point(0x9d, 0);
            this.picDown.Name = "picDown";
            this.picDown.Size = new Size(0x15, 0x15);
            this.picDown.SizeMode = PictureBoxSizeMode.StretchImage;
            this.picDown.Click += new EventHandler(this.picDown_Click);
            this.img.Images.Clear();
            this.img.Images.Add((Icon) manager.GetObject("resource"));
            this.img.Images.Add((Icon) manager.GetObject("resource1"));
            base.AutoScaleDimensions = new SizeF(96f, 96f);
            base.AutoScaleMode = AutoScaleMode.Dpi;
            base.AutoScaleMode = AutoScaleMode.Dpi;
            base.Controls.Add(this.panMain);
            base.Name = "LayerControlForCombox";
            base.Size = new Size(0xb2, 0x15);
            this.panMain.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void LayerAfterSelect(object sender, TreeViewEventArgs e)
        {
            if ((e.Node != null) && (e.Node.Tag.ToString() == "Layer"))
            {
                this.LayerSelect(sender, e);
                this.txt.Text = e.Node.Text;
                this.lc.Visible = false;
            }
        }

        public void OnLayerChanged(object sender, EventArgs e)
        {
            this.LayerSelect(null, null);
            this.txt.Text = "";
        }

        private void OnLayerSelect(object sender, TreeViewEventArgs e)
        {
        }

        private void picDown_Click(object sender, EventArgs e)
        {
            if (!this.isEx)
            {
                this.picDown.Image = this.img.Images[1];
            }
            else
            {
                this.picDown.Image = this.img.Images[0];
            }
            if (this.lc.Visible)
            {
                this.lc.Visible = false;
                this.isEx = false;
            }
            else
            {
                if (base.Parent.Controls.IndexOf(this.lc) == -1)
                {
                    this.lc.Location = new Point(base.Location.X, base.Location.Y + base.Height);
                    this.lc.Name = "ThisLayer";
                    this.lc.Size = new Size(base.Width, 150);
                    this.lc.LayerAfterSelect += new TreeViewEventHandler(this.LayerAfterSelect);
                    base.Parent.Controls.Add(this.lc);
                    base.Parent.Controls.SetChildIndex(this.lc, 0);
                }
                this.isEx = true;
                this.lc.Visible = true;
                this.lc.Focus();
            }
        }

        public override void Refresh()
        {
            base.Refresh();
            if (this.lc != null)
            {
                this.lc.Refresh();
            }
        }

        public void SetLayer(int index)
        {
            this.lc.SetLayer(index);
            if (base.Parent.Controls.IndexOf(this.lc) == -1)
            {
                this.txt.Text = this.lc.GetLayer().Caption;
            }
        }

        private void SizeLayerControl()
        {
            this.lc = new LayerControl();
            this.lc.Visible = false;
        }

        public MapControl Map
        {
            get
            {
                return this._Map;
            }
            set
            {
                this._Map = value;
                this.lc.Map = value;
                if (this._Map.Map != null)
                {
                    this._Map.Map.LayerChanged += new EventHandler(this.OnLayerChanged);
                }
            }
        }
    }
}

